import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('网格列表2'),
        ),
        body: const HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  List<Widget> _initGridViewData() {
    List<Widget> tempList = [];

    for(var i=0; i< 12; i++){
      tempList.add(
          Container(
            alignment: Alignment.center,
            decoration: const BoxDecoration(
                color: Colors.blue
            ),
            child: Text("第${i}个元素", style: const TextStyle(
                fontSize: 20
            )),
          )
      );
    }
    return tempList;
  }

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      padding: const EdgeInsets.all(10),
      mainAxisSpacing: 10,
      crossAxisSpacing: 10,
      crossAxisCount: 2,
      childAspectRatio: 0.5, //宽高比
      children: _initGridViewData(),
    );
  }
}
